﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using LDE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare27.GameScreens
{
	internal class WhiteFadeScreen : GameScreen
	{
		private Texture2D WhiteTex;

		public enum States
		{
			FadingIn,
			FadeInPause,
			FadedIn,
			FadingOut,
		}

		public States State = States.FadingIn;

		public float FadeInTime = 0.1f;
		public float PauseTime = 0.1f;
		public float FadeOutTime = 0.1f;
		private float Timer = 0.0f;

		public WhiteFadeScreen( )
			: base( false )
		{
		}

		public void FadeOut( )
		{
			Timer = 0.0f;

			if ( State == States.FadingIn )
			{
				Timer = FadeOutTime * ( Timer / FadeInTime );
			}

			State = States.FadingOut;
		}

		public override void OnAdded( )
		{
			WhiteTex = ContentManager.Load<Texture2D>( "Img/White" );

			base.OnAdded( );
		}

		public override void Render( float dt, GraphicsDevice gd, SpriteBatch sprites )
		{
			float alpha = 0.0f;

			if ( State == States.FadingIn )
			{
				Timer += dt;
				alpha = Timer / FadeInTime;

				if ( Timer >= FadeInTime )
				{
					State = States.FadeInPause;
					Timer = 0.0f;
				}
			}
			else if ( State == States.FadeInPause )
			{
				Timer += dt;
				if ( Timer >= PauseTime )
				{
					State = States.FadedIn;
				}

				alpha = 1.0f;
			}
			else if ( State == States.FadedIn )
			{
				alpha = 1.0f;
			}
			else if ( State == States.FadingOut )
			{
				Timer += dt;

				if ( Timer >= FadeInTime )
				{
					this.KillScreen = true;
				}

				alpha = 1.0f - ( Timer / FadeOutTime );
			}

#if !SILVERLIGHT
			sprites.Begin( SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.LinearWrap, DepthStencilState.Default,
				RasterizerState.CullCounterClockwise );

			sprites.Draw( WhiteTex,
			   new Microsoft.Xna.Framework.Rectangle( 0, 0, gd.PresentationParameters.BackBufferWidth, gd.PresentationParameters.BackBufferHeight ),
			   Color.White * alpha );

			sprites.End( );
#endif

			base.Render( dt, gd, sprites );
		}
	}
}